layui.config({
    version: '1593348928630' //为了更新 js 缓存，可忽略
});

layui.use(['laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider', 'form'], function () {
    var $ = layui.$
        , layer = layui.layer //弹层
        , table = layui.table //表格
        , form = layui.form
        , element = layui.element
        , userListUrls = jsRoutes.controllers.admin.NasController.userList()
        , groupListUrls = jsRoutes.controllers.admin.NasController.groupList()
        , userLockedUrls = jsRoutes.controllers.admin.NasController.userLocked()
        , userChangeGroupUrls = jsRoutes.controllers.admin.NasController.userChangeGroup()
        , groupCreateUrls = jsRoutes.controllers.admin.NasController.groupCreate()
        , nasGroupsCheck = jsRoutes.controllers.admin.NasController.nasGroupsCheck()
        , nasUsersCheck = jsRoutes.controllers.admin.NasController.nasUsersCheck()

    //向世界问个好
    layer.msg('Hello World');

    //执行一个 table 实例 NAS用户
    function userList(data){
      table.render({
        elem: '#userstable'
        , contentType: 'application/json'
        , method: userListUrls.method
        , height: 550
        , url: userListUrls.url //数据接口
        , title: '用户表'
        , page: true //开启分页
        , where:data
        , toolbar: '#userBarGroupsDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        //, totalRow: true //开启合计行
        , cols: [[ //表头
            { type: 'checkbox', fixed: 'left' }
            , { field: 'id', title: 'ID', width: 60, sort: true, fixed: 'left' }
            , { field: 'username', title: '用户名', width: 130 }
            , { field: 'actived', title: '启用', width: 80, sort: true }
            , { field: 'nas_name', title: 'NAS用户名', width: 180, sort: true }
            , { field: 'group_name', title: 'NAS组名', width: 180, sort: true }
            , { field: 'locked', title: 'NAS禁用', width: 110, sort: true }
            , { field: 'checked', title: '同步', width: 100, sort: true }
            , { fixed: 'right', title: '操作', width: 120, align: 'center', toolbar: '#userBarDemo' }
        ]]
    });
};
 userList({"active":"on","name":""})
    //执行一个 table 实例 NAS组
    var demotable = table.render({
        elem: '#groupstable'
        , contentType: 'application/json'
        , method: groupListUrls.method
        , height: 550
        , url: groupListUrls.url //数据接口
        , title: 'NAS组表'
        , page: true //开启分页
        , where:{"active":"on","name":""}
        , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , totalRow: true //开启合计行
        , cols: [[ //表头
            { type: 'checkbox', fixed: 'left' }
            , { field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left' }
            , { field: 'company', title: '组织', width: 130 }
            , { field: 'group_id', title: '组ID', width: 130 }
            , { field: 'nas_name', title: '组名', width: 180, sort: true }
            , { field: 'pname', title: '部门名', width: 180, sort: true }

        ]]
    });

    //监听头工具栏事件
    table.on('toolbar(test)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id)
            , csdata = checkStatus.data //获取选中的数据
            , layEvent = obj.event;

        if (layEvent === 'changegroup') {
            layer.msg('修改用户组');
            var data1 = form.val("jsonSetGroup");
            var userArr = []
            var gid = data1.gid

            for (i = 0; i < csdata.length; i++) {
                userArr.push({ "id": csdata[i].id, "gid": gid });
            }
            var load_index = layer.load(1, { time: 30 * 1000 });
            $.post({
                url: userChangeGroupUrls.url + "?csrfToken=" + data1.csrfToken,
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(userArr),
                dataType: 'text',
                success: function (data) {
                    console.log(data);
                    layer.close(load_index);
                    layer.msg("数据: \n" + data + "\n状态: " + status);
                    demotable.reload();
                }
            });
        }else if (layEvent  === 'add'){
          layer.msg('新增用户组');
          window.top.location.href = groupCreateUrls.url;
        }


    });

    //监听行工具事件
    table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent === 'onoff') {
            layer.msg('启禁操作');
            var userArr = [];
            userArr.push(data);
            var load_index = layer.load(1, { time: 30 * 1000 });
            $.get({
                url: userLockedUrls.url,
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify({ option: "onoff", id: userArr }),
                dataType: 'text',
                success: function (data) {
                    console.log(data);
                    layer.close(load_index);
                    alert("数据: \n" + data + "\n状态: " + status);
                    demotable.reload();
                }
            });
        } else if (layEvent === 'edit') {
            layer.msg('编辑操作');
            console.log(data.id);
        }
    });

    // 按钮
    form.on('submit(checkusers)', function(data){
      console.log("checkusers");
            $.get({
                url: nasUsersCheck.url,
                dataType: 'text',
                success: function (data,status) {
                    console.log(data);
                    alert("数据: \n" + data + "\n状态: " + status);
                }
            });
    });

    form.on('submit(checkgroups)', function(data){
          console.log('checkgroups');
          $.get({
              url: nasGroupsCheck.url,
              dataType: 'text',
              success: function (data,status) {
                  console.log(data);
                  alert("数据: \n" + data + "\n状态: " + status);
              }
          });
        });

    form.on('submit(formuserfilter)', function(data){
          console.log("checkusers");
          userList(data.field)
           return false;
        });

});